package com.CloudDream.Command.Player;

import com.CloudDream.Inventory.InventoryItem;
import com.CloudDream.MoonCake;
import com.CloudDream.Util.TextUtil;
import com.handy.lib.command.IHandyCommandEvent;
import com.handy.lib.util.BaseUtil;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;

public class GiveCommand implements IHandyCommandEvent {

    private MoonCake instance = MoonCake.getInstance();

    @Override
    public String command() {
        return "givemooncake";
    }

    @Override
    public String permission() {
        return "mooncake.givemooncake";
    }

    public void onCommand(final CommandSender commandSender, Command command, String s, String[] args) {
        if (BaseUtil.isNotPlayer(commandSender).booleanValue()) {
            commandSender.sendMessage(MoonCake.prefix + "§7该命令只能由玩家执行");
            return;
        }

        ItemStack item;
        Player t;
        Player p = (Player) commandSender;
        item = p.getItemInHand();
        t = Bukkit.getPlayer(args[1]);
        String m = "Message.";

        (new BukkitRunnable() {
            @Override
            public void run() {
                if (t != null) {
                    if (t == p) {
                        p.sendMessage(MoonCake.prefix + TextUtil.ReplaceText(m + ".Error-6"));
                        return;
                    }

                    if (item.equals(InventoryItem.getMoonCake(0))) {
                        p.setItemInHand(new ItemStack(Material.AIR));
                        p.sendMessage(MoonCake.prefix + TextUtil.ReplaceTextAll(p, "§6莲蓉月饼"));

                        t.getInventory().addItem(InventoryItem.getMoonCake(0));
                        p.sendMessage(MoonCake.prefix + TextUtil.ReplaceTextAll(t, "§6莲蓉月饼"));
                    }

                    if (item.equals(InventoryItem.getMoonCake(1))) {
                        p.setItemInHand(new ItemStack(Material.AIR));
                        p.sendMessage(MoonCake.prefix + TextUtil.ReplaceTextAll(p, "§c五仁月饼"));

                        t.getInventory().addItem(InventoryItem.getMoonCake(1));
                        t.sendMessage(MoonCake.prefix + TextUtil.ReplaceTextAll(t, "§c五仁月饼"));
                    }

                    if (item.equals(InventoryItem.getMoonCake(2))) {
                        p.setItemInHand(new ItemStack(Material.AIR));
                        p.sendMessage(MoonCake.prefix + TextUtil.ReplaceTextAll(p, "§e蛋黄月饼"));

                        t.getInventory().addItem(InventoryItem.getMoonCake(2));
                        t.sendMessage(MoonCake.prefix + TextUtil.ReplaceTextAll(t, "§e蛋黄月饼"));
                    }

                    if (item.equals(InventoryItem.getMoonCake(3))) {
                        p.setItemInHand(new ItemStack(Material.AIR));
                        p.sendMessage(MoonCake.prefix + TextUtil.ReplaceTextAll(p, "§b冰皮月饼"));

                        t.getInventory().addItem(InventoryItem.getMoonCake(3));
                        t.sendMessage(MoonCake.prefix + TextUtil.ReplaceTextAll(t, "§b冰皮月饼"));

                    }

                    if (item.equals(InventoryItem.getMoonCake(4))) {
                        p.setItemInHand(new ItemStack(Material.AIR));
                        p.sendMessage(MoonCake.prefix + TextUtil.ReplaceTextAll(p, "§d玫瑰月饼"));

                        t.getInventory().addItem(InventoryItem.getMoonCake(4));
                        t.sendMessage(MoonCake.prefix + TextUtil.ReplaceTextAll(t, "§d玫瑰月饼"));
                    }

                    if (item.equals(InventoryItem.getMoonCake(5))) {
                        p.setItemInHand(new ItemStack(Material.AIR));
                        p.sendMessage(MoonCake.prefix + TextUtil.ReplaceTextAll(p, "§a水果月饼"));

                        t.getInventory().addItem(InventoryItem.getMoonCake(5));
                        t.sendMessage(MoonCake.prefix + TextUtil.ReplaceTextAll(t, "§a水果月饼"));
                    }

                    if (item.equals(InventoryItem.getMoonCake(6))) {
                        p.setItemInHand(new ItemStack(Material.AIR));
                        p.sendMessage(MoonCake.prefix + TextUtil.ReplaceTextAll(p, "§6肉松月饼"));

                        t.getInventory().addItem(InventoryItem.getMoonCake(6));
                        t.sendMessage(MoonCake.prefix + TextUtil.ReplaceTextAll(t, "§6肉松月饼"));
                    }
                }

                if (t == null) {
                    p.sendMessage(MoonCake.prefix + TextUtil.ReplacePlayer(m + ".Error-1", args[1]));
                    return;
                }
            }
        }).runTaskAsynchronously(MoonCake.getInstance());
    }
}